package com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.FeesRecordBatchAddReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.FeesRecordBatchExportReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsBookFeesPlanFeesRecordReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsIdListReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.SmsWnsBookFeesPlanFeesRecordService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 学生书杂费记录表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-04-08
 */
@RestController
@RequestMapping("/welcomeNewStudent/feesRecord")
public class SmsWnsBookFeesPlanFeesController {
    @Resource
    private SmsWnsBookFeesPlanFeesRecordService feesRecordService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation("添加学生书杂费记录")
    @PostMapping("/add")
    public ResponseResult addRecord(@RequestBody SmsWnsBookFeesPlanFeesRecordReq req) {
        return feesRecordService.addRecord(req);
    }

    @ApiOperation("删除学生书杂费记录")
    @PostMapping("/del")
    public ResponseResult delRecord(@RequestBody SmsWnsIdListReq req) {
        return feesRecordService.delRecord(req);
    }

    @ApiOperation("修改学生书杂费记录")
    @PostMapping("/update")
    public ResponseResult updateRecord(@RequestBody SmsWnsBookFeesPlanFeesRecordReq req) {
        return feesRecordService.updateRecord(req);
    }

    @ApiOperation("查询学生书杂费记录列表")
    @GetMapping("/list")
    public ResponseResult listRecord(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "planId", required = false) String planId,
                                     @RequestParam(value = "isBack", required = false) Boolean isBack,
                                     @RequestParam(value = "searchStr", required = false) String searchStr) {
        return feesRecordService.listRecord(pageNum, pageSize, planId, isBack, searchStr);
    }

    @ApiOperation(value = "学生书杂费批量导入")
    @PostMapping("/batch/feesRecordAdd")
    public ResponseResult feesRecordBatchAdd(@RequestBody FeesRecordBatchAddReq req, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try {
            Future<ResponseResult> future = feesRecordService.feesRecordBatchAdd(req, userId);
            while (true) {
                if (future.isDone()) {
                    commonCodeEnum = future.get();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

    @ApiOperation("学生书杂费批量导出")
    @PostMapping("/batch/feesRecordExport")
    public ResponseResult feesRecordBatchExport(@RequestBody FeesRecordBatchExportReq req, HttpServletResponse response) {
        ResponseResult responseResult;
        try {
            Future<ResponseResult> future = feesRecordService.feesRecordBatchExport(req, response);
            while (true) {
                if (future.isDone()) {
                    break;
                }
            }
            responseResult = future.get();
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

}

